class Solution {  
public:  
    ListNode* swapPairs(ListNode* head) {  
        if (head == nullptr || head->next == nullptr) {  
            return head;  
        }  
          
        ListNode *dummy = new ListNode(0);  
        dummy->next = head;  
        ListNode *cur = dummy;  
          
        while (cur->next != nullptr && cur->next->next != nullptr) {  
            ListNode *first = cur->next;  
            ListNode *second = cur->next->next;  
              
            // 交换相邻节点  
            first->next = second->next;  
            second->next = first;  
            cur->next = second;  
              
            cur = cur->next->next;  
        }  
          
        return dummy->next;  
    }  
};